﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@model Hangerd.Mvc.ViewModels.PagedListModel<dPet.Application.DataObjects.InventoryBatchDto>


<div ng-controller="InventoryBatchController">
	<div class="page-header">
		<h3>入库记录</h3>
	</div>

	<div class="panel panel-default">
		<div class="panel-heading">
			@using (Html.BeginForm("InventoryBatchRecords", "Inventory", FormMethod.Get, new { @class = "form-inline", onsubmit = "return check_search_form()" }))
			{
				<div class="input-group">
					<span class="input-group-addon">商品条码</span>
					<input type="text" id="txt_product_barcode" name="ProductBarcode" class="form-control input-sm" value="@ViewBag.QueryDto.ProductBarcode" placeholder="点击此处进行扫码" onfocus="showScanBarcodePanel()" />
					<span class="input-group-btn">
						<button id="btn_search" class="btn btn-info btn-sm" type="submit"><span class="glyphicon glyphicon-search"></span> 查 询</button>
					</span>
				</div>
				<a href="@Url.Action("InventoryBatchRecords", "Inventory")" class="pull-right">清空筛选条件</a>
			}
		</div>

		<table class="table table-striped text-center">
			<thead>
				<tr>
					<th class="text-center">批次号</th>
					<th class="text-center">入库时间</th>
					<th class="text-center">操作人</th>
					<th class="text-center">入库清单</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var batch in Model.List)
				{
					<tr>
						<td>@batch.BatchNumber</td>
						<td>@batch.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
						<td>@batch.Operator</td>
						<td>
							<a href="javascript:;" ng-click="showBatchDetail('@batch.Id')">查看</a>
						</td>
					</tr>
				}
			</tbody>
		</table>
	</div>

	@HtmlUtil.BootstrapPager(Model.PageIndex, Model.PageSize, Model.TotalNumber,
		p => Url.Action("InventoryBatchRecords", "Inventory", new
		{
			ProductBarcode = ViewBag.QueryDto.ProductBarcode,
			pageIndex = p
		}))

	<div class="modal fade" id="scan_Modal" tabindex="-1" role="dialog" aria-hidden="true">
		<br /><br /><br /><br />
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<form onsubmit="fillProductBarcode()">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">读取条码</h4>
					</div>
					<div class="modal-body">
						<input id="txt_scaned_barcode" type="text" class="form-control" placeholder="读取条码" ng-model="scanedBarcode" />
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<script type="text/ng-template" id="inventoryBatchDetailsModal">
	<div class="modal-header">
		<button type="button" class="close" ng-click="close();">&times;</button>
		<h4 class="modal-title">入库清单 [ 批次号: {{ batchNumber }} ]</h4>
	</div>
	<div class="modal-body form-horizontal">
		<table class="table table-striped text-center inventory-list">
			<thead>
				<tr>
					<th class="text-center">商品ID</th>
					<th class="text-center">商品条码</th>
					<th class="text-center">商品名称</th>
					<th class="text-center">入库数量</th>
					<th class="text-center">过期日期</th>
				</tr>
			</thead>
			<tbody>
				<tr ng-repeat='batchDetail in inventoryBatchDetails'>
					<td>{{ batchDetail.ProductId }}</td>
					<td>{{ batchDetail.ProductBarcode }}</td>
					<td>{{ batchDetail.ProductName }}</td>
					<td>{{ batchDetail.Quantity }} {{ batchDetail.UnitName }}</td>
					<td>{{ batchDetail.ExpiryTime }}</td>
				</tr>
			</tbody>
		</table>
	</div>
</script>

@section scripts {
	<script type="text/javascript">
		$(document).ready(function () {
			set_left_nav('#ln_InventoryBatchRecords');
		});

		angular.module('dpet').controller('InventoryBatchController', function ($scope, $http, $modal) {
			$scope.showBatchDetail = function (batchId) {
				$http.post('/Ajax/InventoryBatchDetails',
					{
						'batchId': batchId
					})
					.success(function (data) {
						var modalInstance = $modal.open({
							templateUrl: 'inventoryBatchDetailsModal',
							controller: function ($scope, $modalInstance, inventoryBatchDetails) {
								$scope.inventoryBatchDetails = inventoryBatchDetails;
								$scope.batchNumber = inventoryBatchDetails[0].BatchNumber;
								$scope.close = function () {
									$modalInstance.close();
								}
							},
							size: 'lg',
							resolve: {
								inventoryBatchDetails: function () {
									return data;
								}
							}
						});
					})
					.error(function (result) {
						alert("系统繁忙");
					})
			}
		});

		function showScanBarcodePanel() {
			$('#scan_Modal').modal('show');
		}

		$('#scan_Modal').on('shown.bs.modal', function (e) {
			$("#txt_scaned_barcode").val('');
			$('#txt_scaned_barcode').focus();
		});

		function fillProductBarcode() {
			var scanedBarcode = $.trim($("#txt_scaned_barcode").val());

			if (scanedBarcode != '') {
				$("#txt_product_barcode").val(scanedBarcode);
				$('#scan_Modal').modal('hide');
			}
		}

		function check_search_form() {
			if ($.trim($("#txt_product_barcode").val()) == '') {
				alert("请扫描商品条码");
				return false;
			}

			return true;
		}
	</script>
}